package com.cxsw.rms.service.impl;

import com.cxsw.rms.entity.Student;
import com.cxsw.rms.mappers.StudentMapper;
import com.cxsw.rms.pojo.StudentForPage;
import com.cxsw.rms.service.StudentService;
import com.cxsw.rms.utils.Constants;
import com.cxsw.rms.utils.ResultObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * 关于学生信息业务逻辑层
 *
 * @author Han Fuhao
 * @date 2023/05/22
 */
@Service
public class StudentServiceImpl implements StudentService {
    /**
     * 关于学生数据访问层Spring注入
     */
    @Resource
    private StudentMapper studentMapper;

    /**
     * 上传照片保存路径
     */
    @Value("${multipartFile.file.path}")
    private String upLoadPath;

    /**
     * 获取信息学生
     *
     * @param map 地图
     * @return {@link ResultObject}
     */
    @Override
    public Object getStudentForPage(HashMap<String, Object> map) {
        List<StudentForPage> students = studentMapper.selectStudent(map);
        Integer count = studentMapper.selectCount(map);
        HashMap<String, Object> data = new HashMap<>();
        data.put("students", students);
        data.put("count", count);
        return data;
    }
    /**
     * 注册学生
     *
     * @param student 学生
     * @param session 会话
     * @return {@link ResultObject}
     * @throws IOException ioexception
     */
    @Override
    public ResultObject registerStudent(Student student, HttpSession session) throws IOException {
        if (dataInspection(student).getCode().equals(Constants.RESULT_OBJECT_FAILED_CODE)) {
            return dataInspection(student);
        } else {
            fileSave(student, session);
            studentMapper.insertStudent(student);
            return ResultObject.success("注册成功", null);
        }
    }

    /**
     * 通过学生学号获取学生信息
     *
     * @param stuId 学生id
     * @return {@link ResultObject}
     */
    @Override
    public Object getStudentByStuId(Integer stuId) {
        return studentMapper.selectStudentById(stuId);
    }

    /**
     * 更新学生
     *
     * @param student 学生
     * @param stuId   斯图id
     * @param session 会话
     * @return {@link ResultObject}
     */
    @Override
    public ResultObject updateStudent(Student student, Integer stuId, HttpSession session) throws IOException {
        student.setStuId(stuId);
        if (dataInspection(student).getCode().equals(Constants.RESULT_OBJECT_FAILED_CODE)) {
            return dataInspection(student);
        } else {
            studentMapper.updateStudent(student);
            return ResultObject.success("修改成功", null);
        }
    }

    /**
     * 更新学生照片
     *
     * @param stuId 学生证
     * @param session   会话
     */
    @Override
    public void updateStudentImg(Integer stuId, HttpSession session) throws IOException {
        Student student = studentMapper.selectStudentById(stuId);
        fileSave(student, session);
    }

    /**
     * 删除学生
     *
     * @param stuId 学生证
     * @return {@link ResultObject}
     */
    @Override
    public ResultObject delStudent(Integer stuId) {
        Integer integer = studentMapper.deleteStudent(stuId);
        if (integer > 0){
            return ResultObject.success("删除成功！", null);
        }
        return ResultObject.failure("删除失败！");
    }

    /**
     * 数据检查
     *
     * @param student 学生
     * @return {@link ResultObject}
     */
    public ResultObject dataInspection(Student student){
        if (student.getSex().equals("")) {
            return ResultObject.failure("性别不能为空");
        } else if (student.getBirthday().equals("")) {
            return ResultObject.failure("出生日期不能为空");
        } else if (student.getPhoneNumber().equals("")) {
            return ResultObject.failure("电话号码不能为空");
        } else if (student.getIdCard().equals("")) {
            return ResultObject.failure("身份证号不能为空");
        } else if (student.getGrade().equals("")) {
            return ResultObject.failure("成绩不能为空");
        } else if (student.getSchool().equals("")) {
            return ResultObject.failure("学校不能为空");
        } else if (student.getFeature().equals("")) {
            return ResultObject.failure("政治面貌不能为空");
        } else if (student.getAddress().equals("")) {
            return ResultObject.failure("居住地址不能为空");
        }
        return ResultObject.success("", null);
    }

    /**
     * 文件保存
     *
     * @param student 学生
     * @param session 会话
     * @throws IOException ioexception
     */
    public void fileSave(Student student, HttpSession session) throws IOException {
        byte[] fileData = (byte[]) session.getAttribute(Constants.SESSION_USER_IMG);
        FileOutputStream fos = new FileOutputStream(upLoadPath + student.getPhotoPath() + ".jpg");
        fos.write(fileData);
        fos.close();
    }
}
